#ifndef _dispenser_dragonball_
#define _dispenser_dragonball_

#include "dispenser.h"
#include "igslib_iserial_manager.h"
#include "protocol_DragonBall.h"

namespace igslib
{
    class dispenser_Grgn:public CHopper
    { 
		protected:
			static int ID;
			int LocID;
			char setup_ctrl[17]={0};
        public:

            dispenser_Grgn()
            {
                ID++;
				LocID=ID;
                spi = nullptr;
                setup_ctrl[0]=1; 
            }
            ~dispenser_Grgn()
			{
				if(ID>-1)
				 ID--;
			}
            void PrizeOut() 
            { 
                Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
                tmp->SetDispenser(LocID,  nval, "PayOut");
                state = State::prize_enter;			

            }        
            State GetState() 
            {
                Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
                auto st=tmp->GetDispenserSat(LocID);
                if(st!=0)
                {	
                    if(state != State::error_recovery)
                    {
                        state = State::error;
                        if(st == 1)
                            es=ErrorState::jam;
                        else if(st == 2)
                            es=ErrorState::empty;
                    }		
                }
                else if(nval !=0 && state!=State::prize_enter && state!=State::prize_busy )
                {
                    state = State::prize_busy;
                }
                else
                {
                    state = State::idle;
                    es=ErrorState::none;	
                }
                return state; 	
            }

            float GetValue()
            {
                Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
                auto nv = tmp-> GetDispenserNV(LocID);
                nval -= nv;
                return nval;
            }

            void SetHighActive(const bool& value) 
            { 
                ha = value; 
                if( value==true )
                    setup_ctrl[1]=1;
                else
                    setup_ctrl[1]=0;
                Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
                tmp-> SetupDispenser(LocID,setup_ctrl);

            }				
            void SetGPIO(IGPIO* value,IGPIO* value1, IGPIO* value2) 
            { 
                gpio_in = value; 
                gpio_mc = value1;
                gpio_mcc = value2;

                if(!gpio_in )
                    throw_igslib_exception("dispenser sensor GPIO is NULL");			
                setup_ctrl[2]=(char)gpio_in->GetNumber();

                if(!gpio_mc )
                    throw_igslib_exception("dispenser motor GPIO is NULL");			
                setup_ctrl[3]=(char)gpio_mc->GetNumber();

                if(gpio_mcc)
                    setup_ctrl[4]= (char)gpio_mcc->GetNumber();
                Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
                tmp-> SetupDispenser(LocID,setup_ctrl);
            }		
            void SetConfirmRange(const float& min,const float& max) 
            { 					


                if( min<0 || max>65536)
                    throw_igslib_exception("dispenser ConfirmRange set fail");
                confirm = std::make_pair(min,max);
                setup_ctrl[5]= min;
                setup_ctrl[6]=min/256;
                setup_ctrl[7]= max ;
                setup_ctrl[8]=max/256;
                Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
                tmp-> SetupDispenser(LocID,setup_ctrl);

            }
            void SetEmptyTime(const float& value,const float& value1,const float& value2)
            { 	

                if(value<50 || value>65536)
                    throw_igslib_exception("dispenser SetEmptyTime a0 set fail");
                et[0] = value; 
                if(value1<0 || value1>65536)
                    throw_igslib_exception("dispenser SetEmptyTime a1 set fail");
                et[1] = value1; 
                if(value2<0 || value2>65536)
                    throw_igslib_exception("dispenser SetEmptyTime a2 set fail");
                et[2] = value2;
                //util::Log(" value %g \n",value);
                setup_ctrl[9]=value;
                setup_ctrl[10]=value/256;
                setup_ctrl[11]=value1;
                setup_ctrl[12]=value1/256;
                setup_ctrl[13]=value2;
                setup_ctrl[14]=value2/256;
                Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
                tmp-> SetupDispenser(LocID,setup_ctrl);

            }
            void SetEmptyTime(const float& value)
            {
                SetEmptyTime(value,0,0);
            }
            void SetJammedTime(const float& value) 
            { 
                if(value<50 || value>65536)
                    throw_igslib_exception("dispenser JammedTime set fail");
                jt = value; 
                setup_ctrl[15]=value;
                setup_ctrl[16]=value/256;
                Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
                tmp-> SetupDispenser(LocID,setup_ctrl);
            } 	
            void Reset() 	
            { 
                Protocol_DragonBall* tmp = (Protocol_DragonBall*)(spi);
                tmp-> SetDispenser(LocID, 1 ,"Rest");
                state = State::error_recovery;

            }  



    };
    int dispenser_Grgn::ID = -1;
};

#endif
